﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using WebStickyNotes.Contracts;
using WebStickyNotes.Data.Access;
using WebStickyNotes.Helpers;
using WebStickyNotes.Models;

namespace WebStickyNotes.Controllers
{
    public class TaskAuditController : Controller
    {
        private readonly string _connectionType;
        private readonly string _connectionString;
        private readonly int _pageSize;

        private static readonly List<int> AllowedTaskRoleId = new List<int> { 1 };

        public TaskAuditController()
        {
            _connectionType = ConfigurationManager.AppSettings["ConnectionType"];
            _connectionString = ConfigurationManager.ConnectionStrings["WebStickyNotesData"].ConnectionString;
            _pageSize = int.Parse(ConfigurationManager.AppSettings["PageSize"]);
        }

        [HttpGet]
        [MustSignIn]
        public ActionResult Index(int? page)
        {
            var pageNumber = PagingHelper.GetPageNumber(page);

            using (var supplier = new Supplier(_connectionType, _connectionString))
            {
                var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
                if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
                {
                    return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
                }

                List<TaskAudit> results = supplier.DataAccess.GetAllTaskAudits();

                var model = new TaskAuditIndexModel
                {
                    CurrentUser = currentUser,
                    TotalRecords = results.Count,
                    CurrentPage = pageNumber,
                    PageSize = _pageSize,
                    PagingLink = string.Format("{0}?page={1}", Request.Url.AbsolutePath, "{0}"),
                    TaskAudits = PagingHelper.GetPageOfResults(results, _pageSize, pageNumber)
                };


                return View(model);
            }
        }

        [HttpGet]
        [MustSignIn]
        public ActionResult Detail(int id)
        {
            using (var supplier = new Supplier(_connectionType, _connectionString))
            {
                var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
                if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
                {
                    return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
                }

                var model = supplier.DataAccess.GetTaskAudit(id);

                return View(model);
            }
        }

        [HttpGet]
        [MustSignIn]
        public ActionResult TaskAuditSearch(int searchTerm, int? page)
        {
            var pageNumber = PagingHelper.GetPageNumber(page);

            using (var supplier = new Supplier(_connectionType, _connectionString))
            {
                var currentUser = supplier.DataAccess.GetTaskPerson(SecurityHelper.GetSignedInUserId());
                if (!AllowedTaskRoleId.Contains(currentUser.TaskRoleId))
                {
                    return RedirectToAction("AccessDenied", "Home", new { address = HttpContext.Request.Url.AbsoluteUri });
                }

                List<TaskAudit> results = supplier.DataAccess.GetAllTaskAuditsForSearch(searchTerm);

                var model = new TaskAuditIndexModel
                {
                    CurrentUser = currentUser,
                    TotalRecords = results.Count,
                    CurrentPage = pageNumber,
                    PageSize = _pageSize,
                    PagingLink = string.Format("{0}?searchTerm={1}&page={2}", Request.Url.AbsolutePath, searchTerm, "{0}"),
                    TaskAudits = PagingHelper.GetPageOfResults(results, _pageSize, pageNumber)
                };


                return View("Index", model);
            }
        }
    }
}